package com.zgz.park.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zgz.park.common.utils.MyMD5Util;
import com.zgz.park.pojo.Admin;
import com.zgz.park.mapper.AdminMapper;
import com.zgz.park.pojo.common.CommonPageReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author willie
 * @since 2022-12-10
 */
@Service
public class AdminService {

    @Resource
    private AdminMapper adminMapper;

    /**
     * 条件分页查询列表
     * @param queryFilter
     * @return
     */
    public IPage<Admin> getAllAdminByPage(CommonPageReq queryFilter) {
        Map<String, Object> params = queryFilter.getParams();
        if(params.get("mobile") != null){
            if(params.get("mobile").toString() != null){
                params.put("mobile", params.get("mobile").toString());
            }
        }
        IPage<Admin> allAdminByPageVo = adminMapper.getAllAdminByPageVo(
                new Page<Admin>(queryFilter.getPageNo(), queryFilter.getPageSize()), params);
        return allAdminByPageVo;
    }

    /**
     * 根据id获取管理员信息
     * @param id
     * @return
     */
    public Admin getAdminById(String id) {
        Admin admin = adminMapper.getAdminById(id);
        String passwordDb = admin.getPassword();
        //MyMD5Util.validPassword(passwordDb);
        return admin;
    }

    /**
     * 保存活修改
     * @param vo
     * @return
     */
    public boolean saveSubmit(Admin vo) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        //查询是否有该数据
        Admin dbInfo = adminMapper.getAdminById(vo.getId());
        boolean isUpdate = false;
        if(dbInfo != null){
            isUpdate = true;
        }else{
            isUpdate = false;
        }
        Admin admin = new Admin();
        //设置密码

        if(isUpdate){
            //更新数据
            BeanUtils.copyProperties(vo,admin);
            //判断密码是否变更
            if(dbInfo.getPassword().equals(vo.getPassword())){
                admin.setPassword(dbInfo.getPassword());
            }else{
                String md5PasswordVo = MyMD5Util.getEncryptedPwd(vo.getPassword());
                admin.setPassword(md5PasswordVo);
            }
            adminMapper.updateById(admin);
        }else{
            if(vo.getPassword() != null && vo.getPassword() != ""){
                String md5Password = MyMD5Util.getEncryptedPwd(vo.getPassword());
                admin.setPassword(md5Password);
            }
            //新增数据
            admin.setNickName(vo.getNickName());
            admin.setMobile(vo.getMobile());
            admin.setUserName(vo.getUserName());
            admin.setSex(vo.getSex());
            admin.setAge(vo.getAge());
            admin.setAvatar(vo.getAvatar());
            admin.setPlateNum(vo.getPlateNum());
            admin.setIsDisabled(false);
            admin.setIsDeleted(false);
            adminMapper.insert(admin);
        }
        return true;
    }

    /**
     * 禁用或解禁
     * @param id
     * @return
     */
    public boolean invalidOrRecovery(String id) {

        //查询是否有该数据
        Admin dbInfo = adminMapper.getAdminById(id);

        if(dbInfo != null){
            if(!dbInfo.getIsDeleted()){
                Admin admin = new Admin();
                BeanUtils.copyProperties(dbInfo,admin);
                if(dbInfo.getIsDisabled()){
                    admin.setIsDisabled(false);
                }else{
                    admin.setIsDisabled(true);
                }
                adminMapper.updateById(admin);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }

    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    public boolean deleteById(String id) {

        //查询是否有该数据
        Admin dbInfo = adminMapper.getAdminById(id);

        if(dbInfo != null){
            Admin admin = new Admin();
            BeanUtils.copyProperties(dbInfo,admin);
            admin.setIsDeleted(true);
            adminMapper.updateById(admin);
            return true;
        }else{
            return false;
        }
    }
}
